//------------------------------------------------
// FileName:
//			FileUtil.h
// Summary:
//			provide base file operate
// Remark:
//			this version not support MFC
// Author:
//			lixx@santachi.com.cn
// Date:
//			2013-06-05
// Version:
//			1.0.0.0
// Copyright:
//			SANTACHI.COM.CN
// Modify:
//			Name : lixx   2013-06-05
//			Name :  
//------------------------------------------------

#if !defined(AFX_FILEUTIL_H__727B17A0_5514_4A8A_9D4F_957C5A4FF93F__INCLUDED_)
#define AFX_FILEUTIL_H__727B17A0_5514_4A8A_9D4F_957C5A4FF93F__INCLUDED_
#define WIN32_LEAN_AND_MEAN 
#include <Windows.h>
#include <vector>
#include <string>
using namespace std;

#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif

class CFileUtil
{
public:
	//-----------------------------------------------------
	// Summary:
	//			Modify file's extend name at the base 
	//			of original file name 
	// Parameters:
	//			[in,out] szFileName: original file name
	//			[in]     szExtName : extend name
	//			[in]	 uMaxLen   : original buffer size
	// Return:
	//			TRUE : success change file extend name
	//			FALSE: failed if change file extend name
	//-----------------------------------------------------
	static BOOL ChangeFileExtName(LPTSTR szFileName, LPCTSTR szExtName, UINT uMaxLen);

	//-----------------------------------------------------
	// Summary:
	//			Modify file's extend name and save result  
	//			to latter buffer 'szFileName' 
	// Parameters:
	//			[in]     szOldName : original file name
	//			[in]     szExtName : extend name
	//			[in]	 szFileName: result buffer
	//          [in]	 uMaxLen   : result buffer size
	// Return:
	//			TRUE : success change file extend name
	//			FALSE: failed if change file extend name
	//-----------------------------------------------------
	static BOOL ChangeFileExtName(LPCTSTR szOldName, LPCTSTR szExtName, LPTSTR szFileName, UINT uMaxLen);

	//-----------------------------------------------------
	// Summary:
	//			Change file's file name at the base of file  
	//			name 
	// Parameters:
	//			[in]     szFileName: original file name
	//			[in]     szBaseName: file name
	//          [in]	 uMaxLen   : original file max 
	//								 buffer size
	// Return:
	//			TRUE : success change file' file name
	//			FALSE: failed if change file name
	//-----------------------------------------------------
	static BOOL ChangeFileBaseName(LPTSTR szFileName, LPCTSTR szBaseName, UINT uMaxLen);

	//-----------------------------------------------------
	// Summary:
	//			Change file's file name and save result  
	//			to latter buffer 'szFileName' 
	// Parameters:
	//			[in]     szOldName : original file name
	//			[in]     szBaseName: file name
	//			[in]	 szFileName: result buffer
	//          [in]	 uMaxLen   : result buffer size
	// Return:
	//			TRUE : success change file extend name
	//			FALSE: failed if change file extend name
	//-----------------------------------------------------
	static BOOL ChangeFileBaseName(LPCTSTR szOldName, LPCTSTR szBaseName, LPTSTR szFileName, UINT uMaxLen);

	//-----------------------------------------------------
	// Summary:
	//		    check whether path name is legal or not 
	// Parameters:
	//			[in]     szName    : be checked file name
	// Remark:
	//			the szName is relative path or full path
	// Return:
	//			TRUE : if file name is legal
	//			FALSE: if file name is illegal
	//-----------------------------------------------------
	static BOOL CheckFullNameLegal (LPCTSTR szName);

	//-----------------------------------------------------
	// Summary:
	//			Create a file (cant' create parent directory) 
	// Parameters:
	//			[in]     pszFilePath : file path or file name
	// Return:
	//			TRUE : if success 
	//			FALSE: if failed
	//-----------------------------------------------------
	static BOOL CreateFile(LPCTSTR pszFilePath);

	//-----------------------------------------------------
	// Summary:
	//			Get file create time  
	// Parameters:
	//			[in]     pszFilePath : original file name
	//			[out]    sysTime     : file create time
	// Return:
	//			TRUE : if success
	//			FALSE: if failed
	//-----------------------------------------------------
	static BOOL GetFileTime(LPCTSTR pszFilePath, SYSTEMTIME& sysTime);

	//-----------------------------------------------------
	// Summary:
	//			Get the application program path  
	//			include back slash['/' or '\']
	// Parameters:
	//			[out]     szBuffer : store path buffer
	//			[in]      nMaxSize : path buffer size
	// Return:
	//			TRUE : success get application path
	//			FALSE: failed if fail
	//-----------------------------------------------------
	static BOOL GetAppPath(TCHAR* szBuffer, UINT nMaxSize);


	//------------------------------------------------------
	// Summary:
	//			Get system user directory
	// Parameters:
	//			[out]  pFileName: output path
	//			[in]   pName    : input file name
	// Remark:
	//			NULL
	// Return:
	//			NULL
	//------------------------------------------------------
	static void CFileUtil::GetUserPath(TCHAR *pFileName, const TCHAR*pName);

	//-----------------------------------------------------
	// Summary:
	//			Get user directory's sub directory  path
	// Parameters:
	//			[out]    szFullFileName : store path buffer
	//			[in]     nMaxLen        : path buffer size
	//			[in]     szParam        : sub path name
	//							   such "\\SubPath\\test"
	// Return:
	//			TRUE : create sub directory and get user path
	//			FALSE: failed if fail
	//-----------------------------------------------------
	static BOOL GetCurUserDocFilePath(LPTSTR szFullFileName, UINT nMaxLen, LPTSTR szParam);

	//-----------------------------------------------------
	// Summary:
	//			Get window's sub directory  path
	// Parameters:
	//			[out]    szFullFileName : store path buffer
	//			[in]     nMaxLen        : path buffer size
	//			[in]     szParam        : sub path name
	//							   such "\\SubPath\\test"
	// Return:
	//			TRUE : create sub directory and get user path
	//			FALSE: failed if fail
	//-----------------------------------------------------
	static BOOL GetWindowsPath(LPTSTR szFullFileName, UINT nMaxLen, LPTSTR szParam);

	//-----------------------------------------------------
	// Summary:
	//			Get directory's file list  
	// Parameters:
	//			[in] szSearchDir      : the directory to get
	//			[in] bGetFileByExtName: whether by ext name
	//			[in] lpFileExtName    : file ext name
	//          [in] bGetFullPath     : whether get full path
	//          [in] bRecDir	      : whether cycle search
	//          [out]fileList	      : return directory files
	// Remark:
	//			if bGetFileByExtName is false then lpFileExtName
	//			can be ignored,mostly set it null
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static int GetFileList(LPCTSTR szSearchDir,
						   BOOL bGetFileByExtName,
						   LPCTSTR lpFileExtName,
						   BOOL bGetFullPath,
						   BOOL bRecDir,
						   vector<tstring>& fileList);

	//-----------------------------------------------------
	// Summary:
	//			Get file's extend name[not include dot]
	// Parameters:
	//			[in]   szFileName   : the source file name
	//			[in]   lpExtName    : the output buffer
	//			[in]   nLen         : the output buffer size
	// Remark:
	//			the output extend name not include dot
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL GetFileExtName(LPCTSTR szFileName, LPTSTR lpExtName, int nLen);

	//-----------------------------------------------------
	// Summary:
	//			Get file's name[including extend name]
	// Parameters:
	//			[in]   szFileName   : the source file name
	//			[in]   szBuffer     : the output buffer
	//			[in]   nMaxSize     : the output buffer size
	// Remark:
	//			the output file name  include extend name
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL GetFileName(LPCTSTR szFileName, TCHAR* szBuffer, UINT nMaxSize);

	//-----------------------------------------------------
	// Summary:
	//			Get file's title[not including extend name]
	// Parameters:
	//			[in]   szFileName   : the source file name
	//			[in]   szBuffer     : the output buffer
	//			[in]   nMaxSize     : the output buffer size
	// Remark:
	//			the output file name  not include extend name
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL GetFileTitleEx(LPCTSTR szFileName, TCHAR* szBuffer, UINT nMaxSize);

	//-----------------------------------------------------
	// Summary:
	//			slash the directory and output to new buffer
	// Parameters:
	//			[in]   lpDirName    : the source directory name
	//			[in]   szBuffer     : the output buffer
	//			[in]   nMaxSize     : the output buffer size
	// Remark:
	//			add '/' or '\' by condition
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL SlashDir(LPCTSTR lpDirName, TCHAR* szBuffer, UINT nMaxLen);

	//-----------------------------------------------------
	// Summary:
	//			validate whether the file or directory path 
	//			is valid or not
	// Parameters:
	//			[in]   lpszPath   : file or directory path
	// Remark:
	//			NULL
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL IsPathValid(LPCTSTR lpszPath);

	//-----------------------------------------------------
	// Summary:
	//			get the file's parent directory 
	//			is valid or not
	// Parameters:
	//			[in]   szFileName   : the source file name
	//			[in]   szBuffer     : the output buffer
	//			[in]   nMaxSize     : the output buffer size
	// Remark:
	//			the output directory not including slash
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL GetFileDirName (LPCTSTR szFileName, TCHAR* szBuffer, UINT nMaxSize);

	//-----------------------------------------------------
	// Summary:
	//			create all the directory that not exist
	// Parameters:
	//			[in]   szDir   : the source directory name
	// Remark:
	//			the parameter is directory, not file path
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL CreateDirectory(LPCTSTR szDir);

	//-----------------------------------------------------
	// Summary:
	//			check if the file or directory writable
	// Parameters:
	//			[in]   pszPath   : directory or file path
	//			[in]   bDirectory: if path is directory
	// Remark:
	//			NULL
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL IsWriteAble(LPCTSTR pszPath, BOOL bDirectory);

	//-----------------------------------------------------
	// Summary:
	//			delete the directory
	// Parameters:
	//			[in]   lpDirName   : directory  path
	// Remark:
	//			cant delete dir including system or hide file
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL DeleteDir(LPCTSTR lpDirName);

	//-----------------------------------------------------
	// Summary:
	//			delete file
	// Parameters:
	//			[in]   szFileName   : file path
	// Remark:
	//			NULL
	// Return:
	//			TRUE : success 
	//			FALSE: failed 
	//-----------------------------------------------------
	static BOOL DeleteOneFile(LPCTSTR szFileName);

	//-----------------------------------------------------
	// Summary:
	//			check file or directory is exist
	// Parameters:
	//			[in]   lpName   : directory  or file path
	// Remark:
	//			NULL
	// Return:
	//			TRUE : exist 
	//			FALSE: not exist 
	//-----------------------------------------------------
	static BOOL IsFileOrDirExist(LPCTSTR lpName);

	//-----------------------------------------------------
	// Summary:
	//			rename the file or directory
	// Parameters:
	//			[in]   lpOldFileOrPath : old file full path
	//			[in]   lpNewFileOrPath : new file full path
	// Remark:
	//			NULL
	// Return:
	//			TRUE : success 
	//			FALSE: faild 
	//-----------------------------------------------------
	static BOOL Rename(LPCTSTR lpOldFileOrPath, LPCTSTR lpNewFileOrPath);

	//-----------------------------------------------------
	// Summary:
	//			copy file to a directory
	// Parameters:
	//			[in]   lpSourceFile : the source file path
	//			[in]   lpDesFile    : the dest file path
	//			[in]   bFailIfExists: whether fail if exist
	// Remark:
	//			NULL
	// Return:
	//			TRUE : success 
	//			FALSE: faild 
	//-----------------------------------------------------
	static BOOL CopyFile(LPCTSTR lpSourceFile, LPCTSTR lpDesFile, BOOL bFailIfExists);

	//-----------------------------------------------------
	// Summary:
	//			copy directory to other directory
	// Parameters:
	//			[in]   lpSourceDir  : the source directory path
	//			[in]   lpDesFile    : the dest directory
	//			[in]   bFailIfExists: whether fail if exist
	// Remark:
	//			NULL
	// Return:
	//			TRUE : success 
	//			FALSE: faild 
	//-----------------------------------------------------
	static BOOL CopyDirectory(LPCTSTR lpSourceDir, LPCTSTR lpDesDir, BOOL bFailIfExists);

	//-----------------------------------------------------
	// Summary:
	//			Get system temp directory's sub path
	// Parameters:
	//			[in]   lpSourceDir  : the source directory path
	//			[in]   lpDesFile    : the dest directory
	//			[in]   bFailIfExists: whether fail if exist
	// Remark:
	//			if not exist the directory, this will create it
	// Return:
	//			TRUE : success 
	//			FALSE: faild 
	//-----------------------------------------------------
	static BOOL GetTempPath(LPCTSTR lpParam, TCHAR* szBuffer, UINT nMaxSize);


	//-----------------------------------------------------
	// Summary:
	//			get file's modify time
	// Parameters:
	//			[in]   szFileName  : the source file name
	//			[in]   modtime     : the modify time
	// Remark:
	//			NULL
	// Return:
	//			file time : success 
	//			current time: faild 
	//-----------------------------------------------------
	static BOOL GetFileModifyTime(LPCTSTR pszFilePath, SYSTEMTIME& sysModifyTime);

	//-----------------------------------------------------
	// Summary:
	//			Get file' size
	// Parameters:
	//			[in]   szFileName  : the source file name
	//			[out]   pdwHigh    : the high part size
	// Remark:
	//			if file big enough that a long number can't
	//			express it so path the pdwhigh to receive 
	//			the high part size, or set to NULL
	// Return:
	//			file size
	//-----------------------------------------------------
	static LONG GetFileSize(LPCTSTR szFileName, DWORD* pdwHigh);

	//-----------------------------------------------------
	// Summary:
	//			Get file' size
	// Parameters:
	//			[in]   szDirName  : the directory name
	//			[in]   szBaseName : the file name
	//			[in]   szFullFileName  : output buffer
	//			[in]   uMaxLen  : output buffer size
	// Remark:
	//			connect the direcotry and file base name
	// Return:
	//			true if success else false 
	//-----------------------------------------------------
	static BOOL GetFullFileName(LPCTSTR szDirName, LPCTSTR szBaseName, LPTSTR szFullFileName, UINT uMaxLen);

	//-----------------------------------------------------
	// Summary:
	//			change the asc string to unicode string
	// Parameters:
	//			[in]   pSource  : the asc string
	// Remark:
	//			remember delete the return string after use
	// Return:
	//			the unicode string
	//-----------------------------------------------------
	static wchar_t* String2WString(const char* pSource);

	//-----------------------------------------------------
	// Summary:
	//			change the asc string to unicode string
	// Parameters:
	//			[in]   pSource  : the asc string
	// Remark:
	//			no delete return string
	// Return:
	//			the unicode string
	//-----------------------------------------------------
	static wstring String2WString(const string& strSource);

	//-----------------------------------------------------
	// Summary:
	//			change the unicode string to asc string
	// Parameters:
	//			[in]   pSource  : the unicode string
	// Remark:
	//			remember delete the return string after use
	// Return:
	//			the asc string
	//-----------------------------------------------------
	static char* WString2String(const wchar_t* pSource);

	/*
	//-----------------------------------------------------
	// Summary:
	//			Get file version,include exe or dll
	// Parameters:
	//			[in]  szComponentPath : component path
	//			[in]  szBuffer        : version buffer
	//			[in]  nMaxSize        : buffer size
	// Remark:
	//			such as '1.0.0.1'
	// Return:
	//			the asc string
	//-----------------------------------------------------
	static BOOL GetFileVersion(LPCTSTR szComponentPath, TCHAR* szBuffer, UINT nMaxSize);
	*/

	//-----------------------------------------------------
	// Summary:
	//			Get system first hard disk driver name
	// Parameters:
	//			[out]  szDriverName	  : component path
	//			[in]   nMaxSize       : buffer size
	// Remark:
	//			such as '1.0.0.1'
	// Return:
	//			success return true else return false
	//-----------------------------------------------------
	static BOOL GetSystemDriver(LPTSTR szDriverName,UINT nMaxSize);

};

#endif // !defined(AFX_FILEUTIL_H__727B17A0_5514_4A8A_9D4F_957C5A4FF93F__INCLUDED_)
